<?php

declare(strict_types=1);

namespace app\controller\system\permission;

use app\BaseController;
use app\service\system\MenuService;
use owns\exception\TransException;

/**
 * 菜单栏
 */
class Menu extends BaseController
{
    protected $menuService;

    public function initialize()
    {
        $this->menuService = make(MenuService::class);
    }
    /**
     * 管理员菜单
     */
    public function userMenu()
    {
        $result['menu'] = $this->menuService->userMenu($this->request->auth->role_id);
        $result['permissions'] = $this->menuService->roleMenuApi($this->request->auth->role_id, 'code');
        return $this->success($result);
    }
    /**
     * 菜单列表
     */
    public function list()
    {
        $param = $this->request->all();
        return $this->success($this->menuService->list($param));
    }
    /**
     * 菜单编辑
     */
    public function edit()
    {
        $data = $this->request->all();
        $this->validate($data, [
            'id' => 'integer|egt:0',
            'parent_id' => 'integer|egt:0',
            'name' => 'require|length:2,20',
            'type' => 'integer|in:menu,button,link,iframe',
            'sort' => 'integer|egt:1|elt:255',
            'apiList' => 'array',
        ]);
        $id = (int) $data['id'];
        return TransException::exec(MenuService::class, 'edit', $data, $id);
    }
    /**
     * 菜单删除
     */
    public function delete()
    {
        $param = $this->request->all();
        $this->validate($param, ['ids' => 'array']);
        return TransException::exec(MenuService::class, 'delete', $param['ids']);
    }
}
